// pages/index/search/search.js
var http = require('../../../utils/http.js')
const app = getApp()
var $ = require('../../../utils/js.js');
var img_gy_src = $.img_gy_src;
var image_gy_src = $.image_gy_src;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    img_gy_src: img_gy_src,
    image_gy_src: image_gy_src,
    //商家切换
    tab: ["店铺", "动态", "商品"],
    //选中的商家
    tab_num: 1,
    //商家分类
    client: [],
    //历史记录
    lishi: [],
    //搜索框
    sou: "",
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.request("circle");

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  //自定义
  //请求
  request(_type) {
    wx.request({
      url: http.API_ROOT + "common/category",
      header: http.header,
      data: {
        type: _type
      },
      success: (res) => {
        this.setData({
          client: res.data.data
        })
      }
    })
  },
  //店铺切换
  switchover(e) {
    var index = e.target.dataset.index
    if (index == 0) {
      this.request("shop")
    } else if (index == 1) {
      this.request("circle")
    }
    this.setData({
      tab_num: index
    })
  },
  //商家分类跳转
  client_jump(e) {
    //console.log(e.target.dataset.item.id)
    var item = e.target.dataset.item ? e.target.dataset.item : ''
    var id = e.target.dataset.id ? e.target.dataset.id : ''
    console.log(item)
    if (this.data.tab_num == 0) {
      wx.navigateTo({
        url: "/pages/index/search/shang/shang?keyword=" + item + '&cat_id=' + id
      })
    } else if (this.data.tab_num == 1) {
      wx.navigateTo({
        url: "/pages/index/hylb/hylb?keyword=" + item + '&catid=' + id
      })
    } else if (this.data.tab_num == 2) {
      wx.navigateTo({
        url: "/pages/index/search/shang_pin/shang_pin?keyword=" + item + '&catid=' + id
      })
    }
  },
  //清除历史记录
  clear(e) {
    this.data.lishi.splice(e.target.dataset.index, 1)
    wx.setStorage({
      key: "lishi",
      data: this.data.lishi,
    })
    this.setData({
      lishi: this.data.lishi
    })
  },
  //搜索框输入
  sou_input(e) {
    this.setData({
      sou: e.detail.value
    })
  },
  //搜索
  sousuo() {
    var lishi = this.data.lishi;
    this.data.sou.replace(/ /g, '')
    if (this.data.sou) {
      var everyResult = lishi.some((item, index, array) => {
        return item == this.data.sou;
      });
      if (!everyResult) {
        lishi.push(this.data.sou)
      }

      wx.setStorage({
        key: "lishi",
        data: lishi,
      })
    }
    var link = ''
    switch (this.data.tab_num) {
      case 0:
        link = "/pages/index/search/shang/shang?keyword=" + this.data.sou
        break;
      case 1:
        link = "/pages/index/hylb/hylb?keyword=" + this.data.sou
        break;
      case 2:
        link = "/pages/index/search/shang_pin/shang_pin?keyword=" + this.data.sou
        break;
    }

    wx.navigateTo({
      url: link
    })


  },
  //自定义结束
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    wx.getStorage({
      key: "lishi",
      success: (res) => {
        this.setData({
          lishi: res.data
        })
      },
      fail: (e) => {
        console.log(2)
      }
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})